/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.trandio.manager;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.NamingException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.trandio.dao.CitaDAO;
import org.trandio.dao.CitaInterface;
import org.trandio.dao.PacienteDAO;
import org.trandio.dao.PacienteInterface;
import org.trandio.dao.UtilidadesDAO;
import org.trandio.dao.UtilidadesInterface;
import org.trandio.model.PacienteVO;
import org.trandio.model.TurnoDetVO;
import org.trandio.model.TurnoVO;
import org.trandio.util.Utiles;

/**
 *
 * @author root
 */
public class CitasServlet extends HttpServlet {

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    private List<PacienteVO> listPacienteVO;
    private List<TurnoVO> listTurnoVO;
    private List<TurnoDetVO> listTurnoDetVO;
    PacienteInterface pacienteInterface;
    UtilidadesInterface utilidadesInterface;
    CitaInterface citaInterface;

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {

            int opcion = Integer.parseInt(request.getParameter("op"));
            pacienteInterface = new PacienteDAO();
            utilidadesInterface = new UtilidadesDAO();
            citaInterface = new CitaDAO();

            switch (opcion) {

                case 1:
                    try {
                        listPacienteVO = pacienteInterface.consultarPaciente(request.getParameter("cod"), request.getParameter("des"), request.getParameter("des1"));
                    } catch (SQLException ex) {
                        Logger.getLogger(CitasServlet.class.getName()).log(Level.SEVERE, null, ex);
                    } catch (NamingException ex) {
                        Logger.getLogger(CitasServlet.class.getName()).log(Level.SEVERE, null, ex);
                    }

                    Iterator pac = listPacienteVO.iterator();



                    out.println("<table width='100%' style='border:1px solid gray' class='extdt-caption'>");
                    out.println("<tr class='ui-widget-header'><td colspan='5'>Pacientes Encontrados</td></tr>");
                    out.println("<tr class='ui-widget-header'><td>&nbsp;</td><td>Identificaci&oacute;n</td><td>Nombre</td><td>Sexo</td><td>Fecha Nacimiento</td></tr>");

                    while (pac.hasNext()) {
                        PacienteVO b = (PacienteVO) pac.next();

                        out.println("<tr><td><img style='cursor:pointer' onclick='asignarCita(" + b.getId() + ",\"" + b.getNombres() + " " + b.getApellidos() + "\",\"" + b.getFecha_nacimiento() + "\",\"" + b.getNumero_identificacion() + "\")' src='../common/img/asig.png' /></td>"
                                + "<td>" + b.getNumero_identificacion() + "</td>"
                                + "<td>" + b.getNombres() + " " + b.getApellidos() + "</td>"
                                + "<td>" + b.getSexo() + "</td>"
                                + "<td>" + b.getFecha_nacimiento() + "</td>"
                                + "</tr>");
                    }

                    out.println("</table>");

                    break;

                case 2:
                    try {


                        listTurnoVO = citaInterface.consultarTurno(Integer.parseInt(request.getParameter("medico")), Integer.parseInt(request.getParameter("sede")));
                    } catch (SQLException ex) {
                        Logger.getLogger(CitasServlet.class.getName()).log(Level.SEVERE, null, ex);
                    } catch (NamingException ex) {
                        Logger.getLogger(CitasServlet.class.getName()).log(Level.SEVERE, null, ex);
                    }

                    String resp = "";
                    Utiles util = new Utiles();
                    int turnosDisponibles = 0;
                    int turnosAsignados = 0;



                    Iterator turnos = listTurnoVO.iterator();


                    resp = "[";
                    while (turnos.hasNext()) {
                        TurnoVO b = (TurnoVO) turnos.next();
                        try {
                            turnosDisponibles = citaInterface.turnosFecha(Integer.parseInt(request.getParameter("medico")), b.getFecha());
                            turnosAsignados = citaInterface.turnosAsignadosFecha(Integer.parseInt(request.getParameter("medico")), b.getFecha());
                        } catch (SQLException ex) {
                            Logger.getLogger(CitasServlet.class.getName()).log(Level.SEVERE, null, ex);
                        } catch (NamingException ex) {
                            Logger.getLogger(CitasServlet.class.getName()).log(Level.SEVERE, null, ex);
                        }

                        System.out.println("Turnos disponibles: " + turnosDisponibles);
                        System.out.println("Turnos Asignados: " + turnosAsignados);




                        if (turnosAsignados >= turnosDisponibles) {
                            String[] fec = util.split(b.getFecha().toString(), "-");
                            resp += "\"" + Integer.parseInt(fec[1]) + '-' + Integer.parseInt(fec[2]) + '-' + fec[0] + "\",";
                        }

                    }
                    resp += "]";


                    out.println(resp);
                    //out.println("[\"06-08-2011\", \"06-10-2011\", \"06-12-2011\"]");

                    break;


                case 3:
                    try {

                        System.out.println(request.getParameter("fecha_turno"));

                        int medico = Integer.parseInt(request.getParameter("medico"));

                        int sede = Integer.parseInt(request.getParameter("sede"));


                        listTurnoDetVO = citaInterface.consultarDetalleTurno(Integer.parseInt(request.getParameter("medico")), request.getParameter("fecha_turno"), sede);
                    } catch (SQLException ex) {
                        Logger.getLogger(CitasServlet.class.getName()).log(Level.SEVERE, null, ex);
                    } catch (NamingException ex) {
                        Logger.getLogger(CitasServlet.class.getName()).log(Level.SEVERE, null, ex);
                    }

                    Iterator detur = listTurnoDetVO.iterator();



                    out.println("<table width='100%' style='border:1px solid gray' class='extdt-caption'>");
                    out.println("<tr class='ui-widget-header'><td colspan='5'>Citas <span id='tmpfecha'></span></td></tr>");
                    out.println("<tr class='ui-widget-header'><td>&nbsp;</td>"
                            + "<td>Hora Inicio</td>"
                            + "<td>Hora Final</td>"
                            + "<td>Paciente</td>"
                            + "<td>Estado</td></tr>");

                    while (detur.hasNext()) {
                        TurnoDetVO b = (TurnoDetVO) detur.next();


                        String anular = "&nbsp;&nbsp;<img style='cursor:pointer' onclick='anularCita(" + b.getId() + ")' width='16px' height='16px' src='../common/img/del.png' />";
                        String comprobante = "";
                        String tachado = "";


                        if (b.getEstado() == null) {
                            b.setEstado("DISPONIBLE");
                        }

                        if (anular.equals("null") || b.getEstado().equals("ANULADA") || b.getEstado().equals("DISPONIBLE")) {
                            anular = "";
                            tachado = "tachado";
                        }

                        if (b.getEstado().equals("VIGENTE")) {
                            comprobante = "&nbsp;&nbsp;<img style='cursor:pointer' title='Imprimir Comprobante' alt='Imprimir Comprobante' onclick='imprimirCita(" + b.getId() + ")' width='16px' height='16px' src='../common/img/imprimir.gif' />"
                                    + "&nbsp;&nbsp;<img style='cursor:pointer' title='Facturar' alt='Facturar' onclick='facturarCita(" + b.getId() + ")' width='16px' height='16px' src='../common/img/new.gif' />";
                        }


                        out.println("<tr><td><img style='cursor:pointer' onclick='crearCita(" + b.getId() + ",\"" + b.getEstado() + "\")' width='16px' height='16px' src='../common/img/asig.png' />"
                                + anular
                                + comprobante
                                + "</td><td>" + b.getHinicio() + "</td>"
                                + "<td>" + b.getHfinal() + "</td>"
                                + "<td><span class='" + tachado + "'>" + b.getPaciente() + "</span></td>"
                                + "<td>" + b.getEstado() + "</td>"
                                + "</tr>");
                    }

                    out.println("</table>");
                    break;


                case 4:
                    try {
                        citaInterface.asignarCita(Integer.parseInt(request.getParameter("turno")), Integer.parseInt(request.getParameter("paciente")), Integer.parseInt(request.getParameter("paciente")));
                    } catch (SQLException ex) {
                        Logger.getLogger(CitasServlet.class.getName()).log(Level.SEVERE, null, ex);
                    } catch (NamingException ex) {
                        Logger.getLogger(CitasServlet.class.getName()).log(Level.SEVERE, null, ex);
                    }

                    break;

                case 5:
                    try {
                        citaInterface.anularCita(Integer.parseInt(request.getParameter("turno")));
                    } catch (SQLException ex) {
                        Logger.getLogger(CitasServlet.class.getName()).log(Level.SEVERE, null, ex);
                    } catch (NamingException ex) {
                        Logger.getLogger(CitasServlet.class.getName()).log(Level.SEVERE, null, ex);
                    }

                    break;

                case 6:

                    try {
                        listTurnoVO = citaInterface.consultarTurnosOk(Integer.parseInt(request.getParameter("medico")), Integer.parseInt(request.getParameter("sede")));
                    } catch (SQLException ex) {
                        Logger.getLogger(CitasServlet.class.getName()).log(Level.SEVERE, null, ex);
                    } catch (NamingException ex) {
                        Logger.getLogger(CitasServlet.class.getName()).log(Level.SEVERE, null, ex);
                    }


                    Iterator turnosOk = listTurnoVO.iterator();


                    resp = "[";
                    while (turnosOk.hasNext()) {
                        TurnoVO b = (TurnoVO) turnosOk.next();
                        String[] fec = Utiles.split(b.getFecha().toString(), "-");
                        resp += "\"" + Integer.parseInt(fec[1]) + '-' + Integer.parseInt(fec[2]) + '-' + fec[0] + "\",";
                    }

                    resp += "]";
                    out.println(resp);



                    break;

                case 7:
                    try {
                        System.out.println(request.getParameter("fecha_turno"));

                        listTurnoDetVO = citaInterface.consultarDetalleTurno(Integer.parseInt(request.getParameter("medico")), request.getParameter("fecha_turno"), Integer.parseInt(request.getParameter("sede")));
                    } catch (SQLException ex) {
                        Logger.getLogger(CitasServlet.class.getName()).log(Level.SEVERE, null, ex);
                    } catch (NamingException ex) {
                        Logger.getLogger(CitasServlet.class.getName()).log(Level.SEVERE, null, ex);
                    }

                    Iterator detur1 = listTurnoDetVO.iterator();



                    out.println("<table width='100%' style='border:1px solid gray' class='extdt-caption'>");
                    out.println("<tr class='ui-widget-header'><td colspan='5'>Citas <span id='tmpfecha'></span></td></tr>");
                    out.println("<tr class='ui-widget-header'><td>&nbsp;</td>"
                            + "<td>Hora Inicio</td>"
                            + "<td>Hora Final</td>"
                            + "<td>Paciente</td>"
                            + "<td>Estado</td></tr>");

                    while (detur1.hasNext()) {
                        TurnoDetVO b = (TurnoDetVO) detur1.next();


                        String anular = "&nbsp;&nbsp;<img width='16px' height='16px' src='../common/img/del.png' />";
                        String comprobante = "";
                        String tachado = "";


                        if (b.getEstado() == null) {
                            b.setEstado("DISPONIBLE");
                        }

                        if (anular.equals("null") || b.getEstado().equals("ANULADA") || b.getEstado().equals("DISPONIBLE")) {
                            anular = "";
                            tachado = "tachado";
                        }

                        if (b.getEstado().equals("VIGENTE")) {
                            comprobante = "&nbsp;&nbsp;<img style='cursor:pointer' title='Imprimir Comprobante' alt='Imprimir Comprobante' onclick='imprimirCita(" + b.getId() + ")' width='16px' height='16px' src='../common/img/imprimir.gif' />"
                                    + "&nbsp;&nbsp;<img title='Facturar' alt='Facturar' width='16px' height='16px' src='../common/img/new.gif' />";
                        }


                        out.println("<tr><td><img width='16px' height='16px' src='../common/img/asig.png' />"
                                + anular
                                + comprobante
                                + "</td><td>" + b.getHinicio() + "</td>"
                                + "<td>" + b.getHfinal() + "</td>"
                                + "<td><span class='" + tachado + "'>" + b.getPaciente() + "</span></td>"
                                + "<td>" + b.getEstado() + "</td>"
                                + "</tr>");
                    }

                    out.println("</table>");
                    break;

                case 8:
                    try {
                        citaInterface.actualizarEstadoCita(Integer.parseInt(request.getParameter("cita")));
                    } catch (SQLException ex) {
                        Logger.getLogger(CitasServlet.class.getName()).log(Level.SEVERE, null, ex);
                    } catch (NamingException ex) {
                        Logger.getLogger(CitasServlet.class.getName()).log(Level.SEVERE, null, ex);
                    }

                    break;
                //PARA  LOS TURNOS DE USUARIO INDIVIDUAL  

                case 9:
                    try {

                        System.out.println(request.getParameter("fecha_turno"));

                        int medico = Integer.parseInt(request.getParameter("medico"));

                        int sede = Integer.parseInt(request.getParameter("sede"));


                        listTurnoDetVO = citaInterface.consultarDetalleTurnoLibre(Integer.parseInt(request.getParameter("medico")), request.getParameter("fecha_turno"), sede);
                    } catch (SQLException ex) {
                        Logger.getLogger(CitasServlet.class.getName()).log(Level.SEVERE, null, ex);
                    } catch (NamingException ex) {
                        Logger.getLogger(CitasServlet.class.getName()).log(Level.SEVERE, null, ex);
                    }

                    Iterator deturno = listTurnoDetVO.iterator();



                    out.println("<table width='100%' style='border:1px solid gray' class='extdt-caption'>");
                    out.println("<tr class='ui-widget-header'><td colspan='5'>Citas <span id='tmpfecha'></span></td></tr>");
                    out.println("<tr class='ui-widget-header'><td>&nbsp;</td>"
                            + "<td>Hora Inicio</td>"
                            + "<td>Hora Final</td>"
                            + "<td>Paciente</td>"
                            + "<td>Estado</td></tr>");

                    while (deturno.hasNext()) {
                        TurnoDetVO b = (TurnoDetVO) deturno.next();
                        String anular = "";

                        //String anular = "&nbsp;&nbsp;<img style='cursor:pointer' onclick='anularCita(" + b.getId() + ")' width='16px' height='16px' src='../common/img/del.png' />";
                        String comprobante = "";
                        String tachado = "";


                        if (b.getEstado() == null) {
                            b.setEstado("DISPONIBLE");
                        }

                        //if (anular.equals("null") || b.getEstado().equals("ANULADA") || b.getEstado().equals("DISPONIBLE")) {
                        //    anular = "";
                        tachado = "tachado";
                        //}

                        /*
                         * if (b.getEstado().equals("VIGENTE")) { comprobante =
                         * "&nbsp;&nbsp;<img style='cursor:pointer'
                         * title='Imprimir Comprobante' alt='Imprimir
                         * Comprobante' onclick='imprimirCita(" + b.getId() +
                         * ")' width='16px' height='16px'
                         * src='../common/img/imprimir.gif' />" +" ";
                         * //"&nbsp;&nbsp;<img style='cursor:pointer'
                         * title='Facturar' alt='Facturar'
                         * onclick='facturarCita(" + b.getId() + ")'
                         * width='16px' height='16px'
                         * src='../common/img/new.gif' />"; }
                         */


                        out.println("<tr><td><img style='cursor:pointer' onclick='crearCita(" + b.getId() + ",\"" + b.getEstado() + "\")' width='16px' height='16px' src='../common/img/asig.png' />"
                                + anular
                                + comprobante
                                + "</td><td>" + b.getHinicio() + "</td>"
                                + "<td>" + b.getHfinal() + "</td>"
                                + "<td><span class='" + "'>" + b.getPaciente() + "</span></td>"
                                + "<td>" + b.getEstado() + "</td>"
                                + "</tr>");
                    }

                    out.println("</table>");
                    break;


                case 10:
                    try {

                        System.out.println(request.getParameter("fecha_turno"));

                        int medico = Integer.parseInt(request.getParameter("medico"));

                        int sede = Integer.parseInt(request.getParameter("sede"));

                        int paciente = Integer.parseInt(request.getParameter("paciente"));


                        listTurnoDetVO = citaInterface.consultarDetalleTurnoVigentePaciente(Integer.parseInt(request.getParameter("medico")), request.getParameter("fecha_turno"), sede, paciente);
                    } catch (SQLException ex) {
                        Logger.getLogger(CitasServlet.class.getName()).log(Level.SEVERE, null, ex);
                    } catch (NamingException ex) {
                        Logger.getLogger(CitasServlet.class.getName()).log(Level.SEVERE, null, ex);
                    }

                    Iterator deturn = listTurnoDetVO.iterator();



                    out.println("<table width='100%' style='border:1px solid gray' class='extdt-caption'>");
                    out.println("<tr class='ui-widget-header'><td colspan='5'>Citas <span id='tmpfecha'></span></td></tr>");
                    out.println("<tr class='ui-widget-header'><td>&nbsp;</td>"
                            + "<td>Hora Inicio</td>"
                            + "<td>Hora Final</td>"
                            + "<td>Paciente</td>"
                            + "<td>Estado</td></tr>");

                    while (deturn.hasNext()) {
                        TurnoDetVO b = (TurnoDetVO) deturn.next();


                        String anular = "&nbsp;&nbsp;<img style='cursor:pointer' onclick='anularCita(" + b.getId() + ")' width='16px' height='16px' src='../common/img/del.png' />";
                        String comprobante = "";
                        String tachado = "";


                        if (b.getEstado() == null) {
                            b.setEstado("DISPONIBLE");
                        }

                        if (anular.equals("null") || b.getEstado().equals("ANULADA") || b.getEstado().equals("DISPONIBLE")) {
                            anular = "";
                            tachado = "tachado";
                        }

                        if (b.getEstado().equals("VIGENTE")) {
                            comprobante = "&nbsp;&nbsp;<img style='cursor:pointer' title='Imprimir Comprobante' alt='Imprimir Comprobante' onclick='imprimirCita(" + b.getId() + ")' width='16px' height='16px' src='../common/img/imprimir.gif' />";
                            // + "&nbsp;&nbsp;<img style='cursor:pointer' title='Facturar' alt='Facturar' onclick='facturarCita(" + b.getId() + ")' width='16px' height='16px' src='../common/img/new.gif' />";
                        }


                        out.println("<tr><td><img style='cursor:pointer' onclick='crearCita(" + b.getId() + ",\"" + b.getEstado() + "\")' width='16px' height='16px' src='../common/img/asig.png' />"
                                + anular
                                + comprobante
                                + "</td><td>" + b.getHinicio() + "</td>"
                                + "<td>" + b.getHfinal() + "</td>"
                                + "<td><span class='" + tachado + "'>" + b.getPaciente() + "</span></td>"
                                + "<td>" + b.getEstado() + "</td>"
                                + "</tr>");
                    }

                    out.println("</table>");
                    break;

                case 11:
                    try {


                        int medico = Integer.parseInt(request.getParameter("medico"));

                        int sede = Integer.parseInt(request.getParameter("sede"));

                        int paciente = Integer.parseInt(request.getParameter("paciente"));


                        listTurnoDetVO = citaInterface.consultarDetalleTurnoVigentePaciente(Integer.parseInt(request.getParameter("medico")), request.getParameter("fecha_turno"), sede, paciente);
                    } catch (SQLException ex) {
                        Logger.getLogger(CitasServlet.class.getName()).log(Level.SEVERE, null, ex);
                    } catch (NamingException ex) {
                        Logger.getLogger(CitasServlet.class.getName()).log(Level.SEVERE, null, ex);
                    }
                    if (listTurnoDetVO.isEmpty()) {
                        out.println("0");
                    } else {
                        out.println("1");
                    }

                    break;


                case 12:
                    try {

                        System.out.println(request.getParameter("fecha_turno"));

                        int medico = Integer.parseInt(request.getParameter("medico"));

                        int sede = Integer.parseInt(request.getParameter("sede"));


                        listTurnoDetVO = citaInterface.consultarDetalleTurnoInasistente(Integer.parseInt(request.getParameter("medico")), request.getParameter("fecha_turno"), sede);
                    } catch (SQLException ex) {
                        Logger.getLogger(CitasServlet.class.getName()).log(Level.SEVERE, null, ex);
                    } catch (NamingException ex) {
                        Logger.getLogger(CitasServlet.class.getName()).log(Level.SEVERE, null, ex);
                    }

                    Iterator dtr = listTurnoDetVO.iterator();



                    out.println("<table width='100%' style='border:1px solid gray' class='extdt-caption'>");
                    out.println("<tr class='ui-widget-header'><td colspan='5'>Citas <span id='tmpfecha'></span></td></tr>");
                    out.println("<tr class='ui-widget-header'><td>&nbsp;</td>"
                            + "<td>Hora Inicio</td>"
                            + "<td>Hora Final</td>"
                            + "<td>Paciente</td>"
                            + "<td>Estado</td></tr>");

                    while (dtr.hasNext()) {
                        TurnoDetVO b = (TurnoDetVO) dtr.next();
                        String anular = "";

                        //String anular = "&nbsp;&nbsp;<img style='cursor:pointer' onclick='anularCita(" + b.getId() + ")' width='16px' height='16px' src='../common/img/del.png' />";
                        String comprobante = "";
                        String tachado = "";


                        if (b.getEstado() == null) {
                            b.setEstado("DISPONIBLE");
                        }

                        //if (anular.equals("null") || b.getEstado().equals("ANULADA") || b.getEstado().equals("DISPONIBLE")) {
                        //    anular = "";
                        tachado = "tachado";
                        //}

                        /*
                         * if (b.getEstado().equals("VIGENTE")) { comprobante =
                         * "&nbsp;&nbsp;<img style='cursor:pointer'
                         * title='Imprimir Comprobante' alt='Imprimir
                         * Comprobante' onclick='imprimirCita(" + b.getId() +
                         * ")' width='16px' height='16px'
                         * src='../common/img/imprimir.gif' />" +" ";
                         * //"&nbsp;&nbsp;<img style='cursor:pointer'
                         * title='Facturar' alt='Facturar'
                         * onclick='facturarCita(" + b.getId() + ")'
                         * width='16px' height='16px'
                         * src='../common/img/new.gif' />"; }
                         */


                        out.println("<tr><td><img style='cursor:pointer' onclick='marcarAsistenciaCita(" + b.getId() + ")' width='16px' height='16px' src='../common/img/button_ok.png' />"
                                + anular
                                + comprobante
                                + "</td><td>" + b.getHinicio() + "</td>"
                                + "<td>" + b.getHfinal() + "</td>"
                                + "<td><span class='" + "'>" + b.getPaciente() + "</span></td>"
                                + "<td>" + b.getEstado() + "</td>"
                                + "</tr>");
                    }

                    out.println("</table>");
                    break;

                case 13:
                    try {
                        citaInterface.marcarAsistenciaCita(Integer.parseInt(request.getParameter("turno")));
                    } catch (SQLException ex) {
                        Logger.getLogger(CitasServlet.class.getName()).log(Level.SEVERE, null, ex);
                    } catch (NamingException ex) {
                        Logger.getLogger(CitasServlet.class.getName()).log(Level.SEVERE, null, ex);
                    }

                    break;


                case 14:
                    try {

                        System.out.println(request.getParameter("fecha_i"));

                        int medico = Integer.parseInt(request.getParameter("medico"));

                        int sede = Integer.parseInt(request.getParameter("sede"));


                        listTurnoDetVO = citaInterface.consultarDetalleTurnoParaBloquear(medico, request.getParameter("fecturnoi"), request.getParameter("fecturnof"), sede);
                    } catch (SQLException ex) {
                        Logger.getLogger(CitasServlet.class.getName()).log(Level.SEVERE, null, ex);
                    } catch (NamingException ex) {
                        Logger.getLogger(CitasServlet.class.getName()).log(Level.SEVERE, null, ex);
                    }

                    Iterator dtrn = listTurnoDetVO.iterator();



                    out.println("<table width='100%' style='border:1px solid gray' class='extdt-caption'>");
                    out.println("<tr class='ui-widget-header'><td colspan='5'>Citas <span id='tmpfecha'></span></td></tr>");
                    out.println("<tr class='ui-widget-header'><td>&nbsp;</td>"
                            + "<td>Inicio</td>"
                            + "<td>Final</td>"
                            + "<td>Paciente</td>"
                            + "<td>Estado</td></tr>");

                    while (dtrn.hasNext()) {
                        TurnoDetVO b = (TurnoDetVO) dtrn.next();
                        String anular = "";

                        //String anular = "&nbsp;&nbsp;<img style='cursor:pointer' onclick='anularCita(" + b.getId() + ")' width='16px' height='16px' src='../common/img/del.png' />";
                        String comprobante = "";
                        String tachado = "";


                        /*
                         * if (b.getEstado() == null) {
                         * b.setEstado("DISPONIBLE"); }
                         */

                        //if (anular.equals("null") || b.getEstado().equals("ANULADA") || b.getEstado().equals("DISPONIBLE")) {
                        //    anular = "";
                        tachado = "tachado";
                        //}

                        /*
                         * if (b.getEstado().equals("VIGENTE")) { comprobante =
                         * "&nbsp;&nbsp;<img style='cursor:pointer'
                         * title='Imprimir Comprobante' alt='Imprimir
                         * Comprobante' onclick='imprimirCita(" + b.getId() +
                         * ")' width='16px' height='16px'
                         * src='../common/img/imprimir.gif' />" +" ";
                         * //"&nbsp;&nbsp;<img style='cursor:pointer'
                         * title='Facturar' alt='Facturar'
                         * onclick='facturarCita(" + b.getId() + ")'
                         * width='16px' height='16px'
                         * src='../common/img/new.gif' />"; }
                         */

                        if (b.getEstado().equals("DISPONIBLE")) {

                            out.println("<tr><td><img style='cursor:pointer' onclick='BloquearTurnoIndividual(" + b.getId() + ")' width='16px' height='16px' src='../common/img/opencandado.png' />"
                                    + anular
                                    + comprobante
                                    + "</td><td>" + b.getHinicio() + "</td>"
                                    + "<td>" + b.getHfinal() + "</td>"
                                    + "<td><span class='" + "'>" + b.getPaciente() + "</span></td>"
                                    + "<td>" + b.getEstado() + "</td>"
                                    + "</tr>");

                        } else {

                            out.println("<tr><td><img style='cursor:pointer' onclick='desBloquearTurnoIndividual(" + b.getId() + ")' width='16px' height='16px' src='../common/img/closecandado.png' />"
                                    + anular
                                    + comprobante
                                    + "</td><td>" + b.getHinicio() + "</td>"
                                    + "<td>" + b.getHfinal() + "</td>"
                                    + "<td><span class='" + "'>" + b.getPaciente() + "</span></td>"
                                    + "<td>" + b.getEstado() + "</td>"
                                    + "</tr>");
                        }
                    }

                    out.println("</table>");

                    break;

                case 15:
                    int nr = 0;
                    try {

                        System.out.println(request.getParameter("fecha_i"));

                        int medico = Integer.parseInt(request.getParameter("medico"));

                        int sede = Integer.parseInt(request.getParameter("sede"));

                        nr = citaInterface.bloquearTurno(medico, request.getParameter("fecturnoi"), request.getParameter("fecturnof"), sede);
                    } catch (SQLException ex) {
                        Logger.getLogger(CitasServlet.class.getName()).log(Level.SEVERE, null, ex);
                    } catch (NamingException ex) {
                        Logger.getLogger(CitasServlet.class.getName()).log(Level.SEVERE, null, ex);
                    }

                    out.println(nr);

                    break;

                case 16:
                    int nn = 0;
                    try {
                        int turno = Integer.parseInt(request.getParameter("turno"));
                        nn = citaInterface.bloquearTurno(turno);
                    } catch (SQLException ex) {
                        Logger.getLogger(CitasServlet.class.getName()).log(Level.SEVERE, null, ex);
                    } catch (NamingException ex) {
                        Logger.getLogger(CitasServlet.class.getName()).log(Level.SEVERE, null, ex);
                    }

                    out.println(nn);

                    break;
                    
                    
                 case 17:
                    int rn = 0;
                    try {
                        int turno = Integer.parseInt(request.getParameter("turno"));
                        rn = citaInterface.desBloquearTurno(turno);
                    } catch (SQLException ex) {
                        Logger.getLogger(CitasServlet.class.getName()).log(Level.SEVERE, null, ex);
                    } catch (NamingException ex) {
                        Logger.getLogger(CitasServlet.class.getName()).log(Level.SEVERE, null, ex);
                    }

                    out.println(rn);

                    break;
   

            }
        } finally {
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
